<?php
$sMonths = getMonths();
if (!@$iMonth)
{
	$iMonth = date('n');
}
if (!@$iYear)
{
	$iYear = date('Y');
}
$iMonth = (@$_REQUEST['iMonth'] ? $_REQUEST['iMonth'] : date('n'));
$iYear = (@$_REQUEST['iYear'] ? $_REQUEST['iYear'] : date('Y'));
$oUsers = UserHandler::getUsers();
$oJobs = JobHandler::getJobs();
$oTimes = TimeHandler::getTimes($_SESSION['rid'], $iMonth, $iYear);
$sClientNames = ClientHandler::getClientNames();
?>
<form action="index.php" method="post">
<input type="hidden" name="sAction" value="time-sendmonth">
<input type="hidden" name="sView" value="time-list">
<input type="hidden" name="iYear" value="<?php print $iYear ?>">
<input type="hidden" name="iMonth" value="<?php print $iMonth ?>">
<input type="hidden" name="bWholeMonth" value="1">
<table class="boxborder" cellspacing="0">
	<tr>
		<td class="boxhandle" colspan="7"><?php print msg('Times') ?></td>
	</tr>
	<tr>
		<td class="control small" colspan="4" nowrap>
<?php
$iCurrentYear = date('Y');
for ($i = 2001; $i <= $iCurrentYear; $i++)
{
	if ($i > 2001)
	{
		echo ', ';
	}
	if ($i == $iYear)
	{
		echo $i;
	}
	else
	{
		echo '<a href="index.php?sView=time-list&iYear='.$i.'&amp;iMonth='.$iMonth.'">'.$i.'</a>';
	}
}
echo '<br>';
for ($i = 1; $i <= 12; $i++)
{
	if ($i > 1)
	{
		echo ', ';
	}
	if ($i == $iMonth)
	{
		echo $sMonths[$i];
	}
	else
	{
		echo '<a href="index.php?sView=time-list&iYear='.$iYear.'&amp;iMonth='.$i.'">'.$sMonths[$i].'</a>';
	}
}
?>
		</td>
		<td class="control small right" colspan="3"><input type="submit" value="<?php print msg('Send entire month') ?>"></td>
	</tr>
	<tr>
		<td class="listtop small center">
			<a href="javascript: showBox('time-add', 'sView=time-list');"><img src="images/<?php print $_SESSION['oUserVars']['images'] ?>/time-add.png" border="0"></a>
		</td>
		<td class="listtop center nowrap"><?php print msg('Times') ?></td>
		<td class="listtop nowrap"><?php print msg('Task') ?></td>
		<td class="listtop center nowrap"><?php print msg('Project') ?></td>
		<td class="listtop center nowrap"><?php print msg('Client') ?></td>
		<td class="listtop center nowrap"><?php print msg('Price level') ?></td>
		<td class="listtop center nowrap"><?php print msg('Sent') ?></td>
	</tr>
<?php

$iTotalHours = 0;
$oThisTime = reset($oTimes);
while ($oThisTime != false)
{
	if ($oThisTime->iMission) {
		$sMissionTodos = JobHandler::getMissionTodos($oThisTime->iJob);;
	}
	
	$sCoolDate = coolMonth($oThisTime->iStartTime);
	if ($sCoolDate != @$sPrevCoolDate) {
		?>
		<tr>
			<td class="separator small" colspan="7"><?php print $sCoolDate.' ('.date('D', $oThisTime->iStartTime).')' ?></td>
		</tr>
		<?php
		$iHoursInDay = 0;
	}
	$iHours = calcHours($oThisTime->iDuration);
	$iHoursInDay += $iHours;
	$iTotalHours += $iHours;
	?>
	<tr>
		<td class="listentity small center nowrap">
	<?php
	if ($oThisTime->iSent == 0) {
		?>
			<a href="javascript: showBox('time-delete', 'sView=time-list&iTime=<?php print $oThisTime->iID ?>&iMission=<?php print $oThisTime->iMission ?>&iYear=<?php print $iYear ?>&iMonth=<?php print $iMonth ?>');"><img src="images/<?php print $_SESSION['oUserVars']['images'] ?>/time-delete.png" border="0"></a>
			<a href="javascript: showBox('time-change', 'sView=time-list&iTime=<?php print $oThisTime->iID ?>&iMission=<?php print $oThisTime->iMission ?>&iYear=<?php print $iYear ?>&iMonth=<?php print $iMonth ?>');"><img src="images/<?php print $_SESSION['oUserVars']['images'] ?>/time-change.png" border="0"></a>
		<?php
	}
	else {
		echo '<img src="images/blank.gif" width="'.$_SESSION['oUserVars']['images'].'">';
	}
	?>
		</td>
		<td class="listentity center small"><?php print $iHours ?></td>
		<td class="listentity small">
	<?php
	if ($oThisTime->iMission) {
		echo '<a href="index.php?sView=job-view&iJobID='.$oThisTime->iJob.'&amp;iMissionID='.$oThisTime->iMission.'&amp;iWorkID='.$oThisTime->iID.'">'.$sMissionTodos[$oThisTime->iMission].'</a>';
		if ($oThisTime->sDescription && $oThisTime->sDescription != $sMissionTodos[$oThisTime->iMission])
		{
			print '<br>('.stripslashes($oThisTime->sDescription).')';
		}
	}
	else {
		echo stripslashes($oThisTime->sDescription);
	}
	?>
		</td>
		<td class="listentity center small nowrap">
	<?php
	if ($oThisTime->iJob) {
		echo '<a href="index.php?sView=job-view&iJobID='.$oThisTime->iJob.'&amp;sDate='.date('Y-m-d', $oThisTime->iStartTime).'">'.$oJobs[$oThisTime->iJob]->sName.'</a>';
	}
	else {
		echo '[ '.msg('Undefined').' ]';
	}
	?>
		</td>
		<td class="listentity center small">
	<?php
	if ($sClientNames[$oThisTime->sSerial]) {
		print $sClientNames[$oThisTime->sSerial].'<br>('.$oThisTime->sSerial.')';
	}
	else if ($oThisTime->sSerial) {
		print $oThisTime->sSerial;
	}
	else if ($oThisTime->sSocial) {
		print "Kennitala: ".$oThisTime->sSocial;
	}
	else {
		print '&nbsp;';
	}
	?>
		</td>
		<td class="listentity center small" nowrap>
	<?php
	if ($oThisTime->bDriving) {
		print msg('Driving');
	}
	else if ($oThisTime->bOvertime) {
		print msg('Overtime');
	}
	else {
		print msg('Normal');
	}
	?>
		</td>
		<td class="listentity center small" nowrap>
	<?php
	if ($oThisTime->sSerial) {
		switch ($oThisTime->iSent)
		{
			case 2:
				echo '<img src="images/custom/greengem.png">';
				break;
			case 1:
				echo '<img src="images/custom/yellowgem.png">';
				break;
			default:
				echo '<img src="images/custom/redgem.png">&nbsp;&nbsp;';
				echo '<input type="button" value="'.msg('Send').'" onClick="location.href = \'index.php?sView=time-list&sAction=time-sendtime&iTime='.$oThisTime->iID.'\';">';
		}
	}
	else {
		echo '&nbsp;';
	}
	?>
		</td>
	</tr>
	<?php
	$sPrevCoolDate = $sCoolDate;
	$oNextTime = next($oTimes);
	prev($oTimes);
	if (@$oNextTime->iStartTime != $oThisTime->iStartTime)
	{
		?>
		<tr>
			<td class="subentity small" colspan="7"><?php print msg('Times in day') ?>: <?php print $iHoursInDay ?></td>
		</tr>
		<tr>
			<td class="small">&nbsp;</td>
		</tr>
		<?php
	}

	$oThisTime = next($oTimes);
}
?>
	<tr>
		<td class="boxbody center small" colspan="7"><?php print msg('Times total') ?>: <?php print $iTotalHours ?></td>
	</tr>
</table>
</form>

